package edu.mis.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


public class Cart {
    protected Map<Integer,OrderDetail> items;
    protected Map<Integer,OrderDetailCustomer> customers;
    protected Map<Integer, Map<Integer, OrderDetailRoom>> list;


    private double totalPricePerDay;
    private int totalQty;


    public Cart(){
        if(items==null)	{
            items=new HashMap<Integer,OrderDetail>();
        }
        if(customers==null)	{
            customers=new HashMap<Integer,OrderDetailCustomer>();
        }
        if(list==null)	{
            list=new HashMap<Integer,Map<Integer, OrderDetailRoom>>();
        }

    }

    public void addRoomType(Integer roomTypeId,OrderDetail orderitem){
        if(items.containsKey(roomTypeId)){
            OrderDetail _orderitem=items.get(roomTypeId);
            _orderitem.setQty(_orderitem.getQty()+orderitem.getQty());
            items.put(roomTypeId, _orderitem);

        }
        else{
            items.put(roomTypeId, orderitem);
        }
    }
    public boolean addCustomer(Integer customerId,OrderDetailCustomer orderDetailCustomer){
        if(customers.containsKey(customerId)){
            return false;
        }else{
            customers.put(customerId, orderDetailCustomer);
            return true;
        }
    }
    public boolean addRoom(Integer orderListId, Integer roomId,OrderDetailRoom orderDetailRoom){
        Map<Integer, OrderDetailRoom> rooms;
        if(list.containsKey(orderListId) && list.get(orderListId).containsKey(roomId)){
            return false;
        }else{
            if(!list.containsKey(orderListId)){
                rooms = new HashMap<Integer, OrderDetailRoom>();
            }else rooms = list.get(orderListId);
            rooms.put(roomId, orderDetailRoom);
            list.put(orderListId, rooms);
            return true;
        }
    }
    public void clearCart(){
        items.clear();
        customers.clear();
    }
    public void clearRoomCart(){
        list.clear();
    }
    public void clearRoomCart(Integer orderListId){
        list.get(orderListId).clear();
    }
    public void removeRoomType(Integer roomTypeId){
        items.remove(roomTypeId);
    }
    public void removeCustomer(Integer customerId){
        customers.remove(customerId);
    }
    public void removeRoom(Integer orderListId, Integer roomId){
        list.get(orderListId).remove(roomId);
    }
    public void updateCart(Integer roomTypeId, Integer newRoomTypeId, Integer qty){
        OrderDetail orderitem;
        OrderDetail newOrderitem;
        orderitem=items.get(roomTypeId);
        orderitem.setQty(qty);
        if(Objects.equals(roomTypeId, newRoomTypeId)){
            items.put(roomTypeId, orderitem);
        }else if((newOrderitem = items.get(newRoomTypeId)) == null){
            orderitem.setRoomTypeId(newRoomTypeId);
            orderitem.setRoomType(orderitem.findRoomTypeByRoomTypeId(newRoomTypeId));
            orderitem=items.put(newRoomTypeId, orderitem);
            items.remove(roomTypeId);
        }else{
            newOrderitem.setQty(newOrderitem.getQty()+qty);
            items.put(newRoomTypeId, newOrderitem);
            items.remove(roomTypeId);
        }
    }
    public boolean updateRoomCart(Integer orderlistId, Integer roomId, Integer newRoomId){
        Map<Integer, OrderDetailRoom> rooms;
        if((rooms = list.get(orderlistId)) != null){
            OrderDetailRoom orderitem;
            OrderDetailRoom newOrderitem;
            orderitem = rooms.get(roomId);
            if(rooms.get(newRoomId) == null) {
                orderitem.setRoomId(newRoomId);
                orderitem.setRoom(orderitem.findRoomByRoomId(newRoomId));
                rooms.put(newRoomId, orderitem);
                rooms.remove(roomId);
                list.put(orderlistId, rooms);
                return true;
            }else return false;
        }else  return false;
    }


    public double getTotalPricePerDay(){
        totalPricePerDay =0;
        int qty;
        for (OrderDetail orderitem : items.values()) {
            RoomType roomType = orderitem.getRoomType();
            qty = orderitem.getQty();
            totalPricePerDay += roomType.getPrice() * qty;

        }
        return totalPricePerDay;
    }
    public int getTotalQty(){
        totalQty=0;
        for (OrderDetail orderitem : items.values()) {
            RoomType roomType = orderitem.getRoomType();
            totalQty += orderitem.getQty();
        }
        return totalQty;
    }
    public int getNum(){
        return customers.size();
    }

    public Map<Integer, OrderDetail> getItems() {
        return items;
    }

    public void setItems(Map<Integer, OrderDetail> items) {
        this.items = items;
    }
    public Map<Integer, OrderDetailCustomer> getCustomers() {
        return customers;
    }

    public void setCustomers(Map<Integer, OrderDetailCustomer> customers) {
        this.customers = customers;
    }

    public Map<Integer, Map<Integer, OrderDetailRoom>> getList() {
        return list;
    }

    public void setList(Map<Integer, Map<Integer, OrderDetailRoom>> list) {
        this.list = list;
    }
}
